package com.bibrApp.difu.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatDialog;

import com.bibrApp.difu.R;


/**
 * Created by dj on 2019/10/25
 */

public class OneBtnDialog extends AppCompatDialog {

    private OnKeyListener onKeyListener;

    public OnKeyListener getOnKeyListener() {
        return onKeyListener;
    }

    public void setOnKeyListener(OnKeyListener onKeyListener) {
        this.onKeyListener = onKeyListener;
    }
    public OneBtnDialog(Context context, int theme, View view) {
        super(context, theme);

        Window window = getWindow();
        assert window != null;
        WindowManager.LayoutParams params = window.getAttributes();
        int[] sd = ScreenUtils.getScreenDispaly(context);
        params.width = (int) (sd[0] * 0.9);
        params.width = 1000;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
        setContentView(view);
    }

    public static class Builder {
        private EditText oneET;
        private OneBtnDialog dialog;
        private Context context;
        private int theme;
        private String title;
        private String content;
        private String rightBtnStr;
        private OnButtonClick buttonClick;
        private OnGetVerifyCodeClick onGetVerifyCodeClick;
        private boolean titleIsVisible;
        private boolean contentIsVisible;
        private boolean etIsVisible;
        private boolean flIsVisible;
        private boolean etOneIsVisible;
        private boolean titleIVIsVisible;
        private boolean isCanceledOnTouchOutside;
        private OnZxingClickListener mZxingClickListener;


        public boolean isCanceledOnTouchOutside() {
            return isCanceledOnTouchOutside;
        }

        public void setCanceledOnTouchOutside(boolean canceledOnTouchOutside) {
            isCanceledOnTouchOutside = canceledOnTouchOutside;
        }

        public boolean isTitleIVIsVisible() {
            return titleIVIsVisible;
        }

        public void setTitleIVIsVisible(boolean titleIVIsVisible) {
            this.titleIVIsVisible = titleIVIsVisible;
        }

        public boolean isEtOneIsVisible() {
            return etOneIsVisible;
        }

        public void setEtOneIsVisible(boolean etOneIsVisible) {
            this.etOneIsVisible = etOneIsVisible;
        }

        public boolean isFlIsVisible() {
            return flIsVisible;
        }

        public void setFlIsVisible(boolean flIsVisible) {
            this.flIsVisible = flIsVisible;
        }

        public String getRightBtnStr() {
            return rightBtnStr;
        }

        public void setRightBtnStr(String rightBtnStr) {
            this.rightBtnStr = rightBtnStr;
        }

        public boolean isEtIsVisible() {
            return etIsVisible;
        }

        public void setEtIsVisible(boolean etIsVisible) {
            this.etIsVisible = etIsVisible;
        }

        public void setButtonClick(OnButtonClick buttonClick) {
            this.buttonClick = buttonClick;
        }

        public void setOnGetVerifyCodeClick(OnGetVerifyCodeClick onGetVerifyCodeClick) {
            this.onGetVerifyCodeClick = onGetVerifyCodeClick;
        }

        public Builder(Context context, int theme) {
            this.context = context;
            this.theme = theme;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public void setZXingCode(String code) {
            if (oneET != null) {
                oneET.setText(code);
            }
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public boolean isTitleIsVisible() {
            return titleIsVisible;
        }

        public void setTitleIsVisible(boolean titleIsVisible) {
            this.titleIsVisible = titleIsVisible;
        }

        public boolean isContentIsVisible() {
            return contentIsVisible;
        }

        public void setContentIsVisible(boolean contentIsVisible) {
            this.contentIsVisible = contentIsVisible;
        }

        public OneBtnDialog creater() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.dialog_one_btn, null);
            dialog = new OneBtnDialog(context, theme, view);
            if(isCanceledOnTouchOutside){
                dialog.setCanceledOnTouchOutside(false);//点击其他区域不可关闭弹窗
                dialog.setCancelable(false);
            }else{
                dialog.setCanceledOnTouchOutside(true);//点击其他区域可关闭弹窗
            }



            oneET = view.findViewById(R.id.dialog_et_one);
//            LinearLayout titleIV = view.findViewById(R.id.dialog_title_iv);
            Button sureBtn = view.findViewById(R.id.dialog_btn_sure);
            TextView titletv = view.findViewById(R.id.dialog_title);
            TextView contentTV = view.findViewById(R.id.dialog_content);
            EditText contentET = view.findViewById(R.id.dialog_et);
            FrameLayout inputCodeFL = view.findViewById(R.id.dialog_input_verify_code_fl);
            EditText codeET = view.findViewById(R.id.dialog_security_code_et);
            TextView getCodeTV = view.findViewById(R.id.dialog_verify_code_get_code_tv);
            ImageView ivZXing = view.findViewById(R.id.dialog_zxing);
            LinearLayout zXingLL = view.findViewById(R.id.dialog_et_one_ll);

           /* if(titleIVIsVisible){
                titleIV.setVisibility(View.VISIBLE);
            }else{
                titleIV.setVisibility(View.GONE);
            }*/
            if (isFlIsVisible()) {
                inputCodeFL.setVisibility(View.VISIBLE);
            } else {
                inputCodeFL.setVisibility(View.GONE);
            }
            if (isEtOneIsVisible()) {
                zXingLL.setVisibility(View.VISIBLE);
                oneET.setVisibility(View.VISIBLE);
            } else {
                zXingLL.setVisibility(View.GONE);
                oneET.setVisibility(View.GONE);
            }
            if (titleIsVisible) {
                titletv.setVisibility(View.VISIBLE);
            } else {
                titletv.setVisibility(View.GONE);
            }
            if (contentIsVisible) {
                contentTV.setVisibility(View.VISIBLE);
            } else {
                contentTV.setVisibility(View.GONE);
            }
            if (etIsVisible) {
                contentET.setVisibility(View.VISIBLE);
            } else {
                contentET.setVisibility(View.GONE);
            }
            titletv.setText(title);
            contentTV.setText(content);
            sureBtn.setText(rightBtnStr);


            getCodeTV.setOnClickListener(v -> {
                if (onGetVerifyCodeClick != null && isFlIsVisible()) {
                    onGetVerifyCodeClick.onClickGetVerifyCode(getCodeTV);
                }
            });

            sureBtn.setOnClickListener(v -> {
                if(buttonClick != null && titleIVIsVisible){
                    buttonClick.onOk();
                }
                if (buttonClick != null && isEtIsVisible() && isEtOneIsVisible()) {
                    String addressStr = oneET.getText().toString().replaceAll(" ", "");
                    String remarkStr = contentET.getText().toString().replaceAll(" ", "");
                    if (TextUtils.isEmpty(remarkStr)) {
                        buttonClick.onOk(addressStr, "");
                    } else {
                        buttonClick.onOk(addressStr, remarkStr);
                    }

                }
            });

            ivZXing.setOnClickListener(v -> {
                if (mZxingClickListener != null) {
                    mZxingClickListener.onClickZxing();
                }
            });
            return dialog;
        }


        public void dismiss() {
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }

        }

        public interface OnZxingClickListener {
            void onClickZxing();
        }

        public void setZxingClickListener(OnZxingClickListener zxingClickListener) {
            this.mZxingClickListener = zxingClickListener;
        }
    }

    @Override
    public void onBackPressed() {
        if(onKeyListener != null){
            onKeyListener.key();
        }
        super.onBackPressed();
    }

    public interface OnButtonClick {
        void onOk(String checkCode, String remark);
        void onOk();
    }

    public interface OnGetVerifyCodeClick {
        void onClickGetVerifyCode(TextView tv);
    }
    public interface OnKeyListener {
        boolean key();
    }
}
